package com.ptswitch.material.web.action;

import java.io.File;

import org.apache.commons.io.FileUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;

import com.ptswitch.material.model.Bom;
import com.ptswitch.material.model.Upload;
import com.ptswitch.material.util.Constant;

@Controller("bomUploadAction")
public class BomUploadAction extends UploadAction {

	private Bom bom;

	public String toImportBom() {
		mustAdmins();
		bom = null;
		return "import";
	}

	public String importBom() {
		mustAdmins();
		Bom dbBom = bomSer.get(bom.getCode(), bom.getVersion());
		if (dbBom != null) {
			bom.setId(dbBom.getId());
			putMsg("新增单板表失败：已存在同名同版本号的单板表，请点击修改。\t");
			return "display";
		}
		uploads();
		if (uploads.isEmpty()) {
			return "import";
		}
		Upload upload = uploads.get(0);
		try {
			excelSer.read(ActionSupport.getSavePath(), upload.getFilePath());
			excelSer.buildBomFromExcel(bom);
		} catch (Throwable e) {
			FileUtils.deleteQuietly(new File(ActionSupport.getSavePath() + upload.getFilePath()));
			putMsg("读取单板表文件错误：" + e.getMessage());
			logger.error("import bom error", e);
			return "import";
		}
		// db
		try {
			bom.setUpload(upload);
			bom.setFileName(upload.getFilePath());
			bomSer.saveEx(bom);
			hlogSer.log(visitor(), Constant.addBom, bom);
			return "display";
		} catch (Throwable e) {
			if (e instanceof DataAccessException) {
				e = e.getCause().getCause();
			}
			putMsg("导入单板表错误：" + e.getMessage());
			logger.error("import bom error", e);
			return "import";
		}
	}

	// to edit
	public String toUpdate() {
		mustAdmins();
		bom = bomSer.get(id);
		if (bom == null) {
			throw new RuntimeException("单板表不存在");
		}
		return "update";
	}

	public String update() {
		mustAdmins();
		Bom dbBom = bomSer.get(bom.getCode(), bom.getVersion());
		if (dbBom != null && !dbBom.getId().equals(bom.getId())) {
			bom.setId(dbBom.getId());
			putMsg("修改单板表不成功：已存在如下同名同版本号的单板表，请点击修改。\t");
			return "display";
		}
		uploads();
		if (uploads.isEmpty()) {
			bomSer.save(bom);
			hlogSer.log(visitor(), Constant.updateBom, bom);
			return "display";
		}
		// bomSer.delete(bom);
		Upload upload = uploads.get(0);
		Bom updateBom = new Bom();
		updateBom.setCode(bom.getCode());
		updateBom.setVersion(bom.getVersion());
		updateBom.setRemark(bom.getRemark());
		try {
			excelSer.read(ActionSupport.getSavePath(), upload.getFilePath());
			excelSer.buildBomFromExcel(updateBom);
		} catch (Throwable e) {
			FileUtils.deleteQuietly(new File(ActionSupport.getSavePath() + upload.getFilePath()));
			putMsg("读取单板表文件错误：" + e.getMessage());
			logger.error("import bom error", e);
			return "update";
		}
		// db
		try {
			updateBom.setId(null);
			updateBom.setFileName(upload.getFilePath());
			updateBom.setUpload(upload);
			bomSer.update(bom, updateBom);
			bom = updateBom;
			hlogSer.log(visitor(), Constant.updateBom, updateBom);
			return "display";
		} catch (Throwable e) {
			if (e instanceof DataAccessException) {
				e = e.getCause().getCause();
			}
			putMsg("更新单板表错误：" + e.getMessage());
			logger.error("import bom error", e);
			return "update";
		}
	}

	// validate
	public String toValidate() {
		bom = null;
		return "validate";
	}

	public String validate() {
		uploads();
		if (uploads.isEmpty()) {
			return "validate";
		}
		bom = new Bom();
		bom.setCode("validate");
		bom.setVersion("validate");
		Upload upload = uploads.get(0);
		upload.setUser(null);// to fix
		try {
			excelSer.read(ActionSupport.getSavePath(), upload.getFilePath());
			excelSer.buildBomFromExcel(bom);
		} catch (Throwable e) {
			FileUtils.deleteQuietly(new File(ActionSupport.getSavePath() + upload.getFilePath()));
			putMsg("读取单板表文件错误：" + e.getMessage());
			logger.error("validate bom error", e);
			return "validate";
		}
		try {
			bom.setUpload(upload);
			bomSer.saveEx(bom);
		} catch (Throwable e) {
			if (e instanceof DataAccessException) {
				e = e.getCause().getCause();
			}
			putMsg("校验单板表异常：" + e.getMessage());
			return "validate";
		}
		bom = bomSer.get(bom.getId());// init bm.material
		excelSer.outputBom(bom);
		String outFilePath = ActionSupport.getSavePath() + "output/validate.xls";
		excelSer.write(outFilePath);
		this.upload = new Upload();
		this.upload.setFilePath("output/validate.xls");
		this.upload.setFileName("validate.xls");
		this.upload.setFileType("application/vnd.ms-excel");
		bomSer.delete(bom);
		FileUtils.deleteQuietly(new File(ActionSupport.getSavePath() + upload.getFilePath()));
		bom = null;
		return "download";
	}

	// get and set

	public Bom getBom() {
		if (bom == null) {
			bom = new Bom();
		}
		return bom;
	}

}
